﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PCMSofCC;

namespace PCMSofCC.BLL
{
    public static class Charts
    {
        public static IQueryable<ScoreChart> GetCharts()
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var q =
                from c in db.ScoreChart
                select c;
            return q;
        }

        public static void AddChart(string _chartName, List<int> _entries)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var newChart = new ScoreChart
            {
                Name = _chartName
            };
            db.ScoreChart.InsertOnSubmit(newChart);
            foreach (int id in _entries)
            {
                var newBelong = new EntryBelongToChart
                {
                    ScoreChart = newChart,
                    EntryId = id
                };
                db.EntryBelongToChart.InsertOnSubmit(newBelong);
            }
            db.SubmitChanges();
        }

        public static void AddEntries(int _chartId, List<int> _entries)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            foreach (int id in _entries)
            {
                var newBelong = new EntryBelongToChart
                {
                    ChartId = _chartId,
                    EntryId = id
                };
                db.EntryBelongToChart.InsertOnSubmit(newBelong);
            }
            db.SubmitChanges();
        }

        public static void DeleteEntries(int _chartId)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var q =
                from b in db.EntryBelongToChart
                where b.ChartId == _chartId
                select b;
            db.EntryBelongToChart.DeleteAllOnSubmit(q);
            db.SubmitChanges();
        }
    }
}
